package com.gton.person.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gton.person.commons.ResultAdvice;
import com.gton.person.entity.Student;
import com.gton.person.entity.StudentUserInfo;
import com.gton.person.service.StudentService;
import com.gton.person.service.StudentUserInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author GuoTong
 * @since 2020-12-18
 */
@RestController
@RequestMapping("/student-user-info")
public class StudentUserInfoController {

    /**
     * 1.注册用户业务处理：用户信息表
     */
    @Autowired
    private StudentUserInfoService studentUserInfoService;

    /**
     * 用户注册表
     */
    @Autowired
    private StudentService studentService;

    /**
     * 用户真实注册
     *
     * @RequestBody StudentUserInfo studentUserInfo,@RequestBody Student student
     */
    @ApiOperation(value = "register上传", notes = "register上传")
    @PostMapping("/register/user")
    @ResponseBody
    public ResultAdvice registerUserTable(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        /*大注册，涉及三张表：声明式事物控制*/
        ResultAdvice resultAdvice = studentUserInfoService.userAndRoleRegister(map);
        //添加返回类型，验证码返回||前台注册完成之后方便自动登录操作
        resultAdvice.setData(request.getSession().getAttribute("captcha"));
        return resultAdvice;

    }

    /**
     * 通过SpringSecurity获取当前用户信息
     */
    @ApiOperation(value = "登录信息获取", notes = "根据登录信息获取用户具体信息")
    @GetMapping("/login/user")
    @ResponseBody
    public ResultAdvice getLoginUserInfo(HttpServletRequest request) {
        //通过SpringSecurity获取当前用户信息
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = "";
        if (principal instanceof UserDetails) {
            username = ((UserDetails) principal).getUsername();
        } else {
            username = principal.toString();
        }
        System.out.println(username);
        //判断是否获取成功
        if (!StringUtils.isEmpty(username)) {
            //条件构造器：1.登录表获取登录表全部信息
            QueryWrapper<Student> wrapper = new QueryWrapper<>();
            wrapper.eq("username", username);
            //根据条件获取当前登录表信息
            Map<String, Object> studentServiceMap = studentService.getMap(wrapper);
            studentServiceMap.forEach((x, y) -> System.out.println(x + ":" + y));
            if (!"".equals(studentServiceMap.get("id")) && studentServiceMap.get("id") != null) {
                //拿到ID，作为外键去实际详细信息表查询
                QueryWrapper<StudentUserInfo> studentUserInfoQueryWrapper = new QueryWrapper<>();
                //找到这个元组
                studentUserInfoQueryWrapper.eq("sid", studentServiceMap.get("id"));
                StudentUserInfo studentUserInfo = studentUserInfoService.getOne(studentUserInfoQueryWrapper);
                request.getSession().setAttribute(studentUserInfo.getUseName(),studentUserInfo.getId());
                String headImage = studentUserInfo.getHeadImage();
                return ResultAdvice.OK(headImage);
            }


        }
        return ResultAdvice.error("未登录");

    }


}

